// 1_ Prepare variables 2010
// //////////////////////////////////

// This do-file uses survey data to generate relevant variables for further analysis.

// ////////////////////////////////////////////////////////////////////////
// INDEX
// I.	Consumer price index, depending on month of interview
// II.	Labor income
// 	II.1. Labor income - primary occupation
// 	II.2. Labor income - secondary occupation
//  II.3. Total labor income 
// 	II.4. Income coming from dependent and independent activities	
// III.	Wage employment, Self-employment and Employment (defined by income of those activities)
// IV.	Weekly hours worked
// V. 	Demographics
// VI. 	Business Practices
// VII. Assets
// VIII.Labeling

// ////////////////////////////////////////////////////////////////////////


clear all
set more off

capture log close

log using "${path_logs}/1_Prepare variables 2010.log", replace

use "${path_data}/data2010", clear


// ////////////////////////////////////////////////////////////////////////
// I. Consumer price index, depending on month of interview
// ////////////////////////////////////////////////////////////////////////

	gen ipc_nov2009=100.30
	generat ipc= 101.87 if date_month==7
	replace ipc= 101.77 if date_month ==8
	replace ipc= 102.18 if date_month ==9
	replace ipc= 102.28 if date_month ==10

	
// ////////////////////////////////////////////////////////////////////////	
// II. Labor income
// ////////////////////////////////////////////////////////////////////////

	// II.1. Labor income - primary occupation
			* Labor income - primary occupation - wage (monthly for wage earners according to d05)
			quiet gen ya1_sueldo=0 if ind_c2010==1
			quiet replace ya1_sueldo= ya1_sueldo + d06m if (d06m!=. & d06m!=88 & d06m!=99)
			quiet summ d06m if (d06m<=5000 & d06m!=88 & d06m!=99) , de 
			quiet gen rest=1 if (d06m==. | d06m==88 | d06m==99)  /*restriction for this variable*/		
			quiet replace ya1_sueldo =ya1_sueldo+ r(p50) if d06t==1 & rest==1 /*imputation of median*/		
			quiet replace ya1_sueldo =ya1_sueldo+ 7500 if d06t==2 & rest==1
			quiet replace ya1_sueldo= ya1_sueldo+ 12500 if d06t==3 & rest==1
			quiet replace ya1_sueldo= ya1_sueldo+ 22500 if d06t==4 & rest==1
			quiet replace ya1_sueldo= ya1_sueldo + 40000 if d06t==5 & rest==1
			quiet replace ya1_sueldo= ya1_sueldo+ 75000 if d06t==6 & rest==1
			quiet replace ya1_sueldo= ya1_sueldo+ 125000 if d06t==7 & rest==1
			quiet replace ya1_sueldo= ya1_sueldo + 175000 if d06t==8 & rest==1
			quiet replace ya1_sueldo= ya1_sueldo + 250000 if d06t==9 & rest==1
			quiet replace ya1_sueldo= ya1_sueldo + 350000 if d06t==10 & rest==1
			quiet replace ya1_sueldo=ya1_sueldo+ 450000 if d06t==11 & rest==1
			quiet replace ya1_sueldo=ya1_sueldo+ 550000 if d06t==12 & rest==1
			quiet replace ya1_sueldo=ya1_sueldo+ 650000 if d06t==13 & rest==1
			quiet replace ya1_sueldo=ya1_sueldo+ 750000 if d06t==14 & rest==1
			quiet replace ya1_sueldo=ya1_sueldo+ 850000 if d06t==15 & rest==1
			quiet replace ya1_sueldo=ya1_sueldo+ 950000 if d06t==16 & rest==1
			quiet replace ya1_sueldo=ya1_sueldo+ 1250000 if d06t==17 & rest==1
			quiet replace ya1_sueldo=ya1_sueldo+ 2250000 if d06t==18 & rest==1
			quiet summ d06m if (d06m>3000000 & d06m!=.), de 
			quiet replace ya1_sueldo=ya1_sueldo+  max(3000000,r(p50)) if d06t==19 & rest==1 /*imputation of median or CLP 3000000*/
			replace ya1_sueldo=. if rest==1 & (d06t==88 | d06t==99)
			replace ya1_sueldo=. if (d05==3 | d05==4 | d05==6 | d05==7 | d05==9) & d06m==. & d06t==. 
			replace ya1_sueldo=0 if d05==8 & ya1_sueldo==.
			drop rest 
			
			* Labor income - primary occupation - business (para indepentents and employers, variable d07)
			quiet gen ya1_negocio=0 if ind_c2010==1
			quiet replace ya1_negocio=ya1_negocio + d07m if (d07m!=. & d07m!=88 & d07m!=99)
			quiet summ d07m if (d07m<=5000 & d07m!=88 & d07m!=99) , de 
			quiet gen rest=1 if (d07m==. | d07m==88 | d07m==99)
			quiet replace ya1_negocio=ya1_negocio+ r(p50) if d07t==1 & rest==1
			quiet replace ya1_negocio=ya1_negocio+ 7500 if d07t==2 & rest==1
			quiet replace ya1_negocio=ya1_negocio + 12500 if d07t==3 & rest==1
			quiet replace ya1_negocio=ya1_negocio + 22500 if d07t==4 & rest==1
			quiet replace ya1_negocio=ya1_negocio + 40000 if d07t==5 & rest==1
			quiet replace ya1_negocio=ya1_negocio + 75000 if d07t==6 & rest==1
			quiet replace ya1_negocio=ya1_negocio + 125000 if d07t==7 & rest==1
			quiet replace ya1_negocio=ya1_negocio + 175000 if d07t==8 & rest==1
			quiet replace ya1_negocio=ya1_negocio + 250000 if d07t==9 & rest==1
			quiet replace ya1_negocio=ya1_negocio + 350000 if d07t==10 & rest==1
			quiet replace ya1_negocio=ya1_negocio+ 450000 if d07t==11 & rest==1
			quiet replace ya1_negocio=ya1_negocio+ 550000 if d07t==12 & rest==1
			quiet replace ya1_negocio=ya1_negocio+ 650000 if d07t==13 & rest==1
			quiet replace ya1_negocio=ya1_negocio+ 750000 if d07t==14 & rest==1
			quiet replace ya1_negocio=ya1_negocio+ 850000 if d07t==15 & rest==1
			quiet replace ya1_negocio=ya1_negocio+ 950000 if d07t==16 & rest==1
			quiet replace ya1_negocio=ya1_negocio+ 1250000 if d07t==17 & rest==1
			quiet replace ya1_negocio=ya1_negocio+ 2250000 if d07t==18 & rest==1
			quiet summ d07m if (d07m>3000000 & d07m!=.) , de 
			replace ya1_negocio =ya1_negocio+ max(3000000,r(p50)) if d07t==19 & rest==1 /*imputation of median or CLP 3000000*/
			replace ya1_negocio=. if rest==1 & (d07t==88 | d07t==99)
			drop rest	
			replace ya1_negocio=. if (d05==1 | d05==2) & d07m==. & d07t==.
			
						
			* Labor income - primary occupation - business products withdrew for personal expenses (for indepentents and employers variable d08)
			quiet gen ya1_productos=0 if ind_c2010==1
			quiet replace ya1_productos=ya1_productos + d08m if (d08m!=. & d08m!=88 & d08m!=99)
			quiet summ d08m if (d08m<=5000 & d08m!=88 & d08m!=99) , de 
			qui gen rest = 1 if (d08m==. | d08m==88 | d08m==99)
			quiet replace ya1_productos=ya1_productos + r(p50) if d08t==1 & rest==1
			quiet replace ya1_productos=ya1_productos + 7500 if d08t==2 & rest==1
			quiet replace ya1_productos=ya1_productos + 12500 if d08t==3 & rest==1
			quiet replace ya1_productos=ya1_productos + 22500 if d08t==4 & rest==1
			quiet replace ya1_productos=ya1_productos + 40000 if d08t==5 & rest==1
			quiet replace ya1_productos=ya1_productos + 75000 if d08t==6 & rest==1
			quiet replace ya1_productos=ya1_productos + 125000 if d08t==7 & rest==1
			quiet replace ya1_productos=ya1_productos + 175000 if d08t==8 & rest==1
			quiet replace ya1_productos=ya1_productos + 250000 if d08t==9 & rest==1
			quiet replace ya1_productos=ya1_productos + 350000 if d08t==10 & rest==1
			quiet replace ya1_productos=ya1_productos+ 450000 if d08t==11 & rest==1
			quiet replace ya1_productos=ya1_productos+ 550000 if d08t==12 & rest==1
			quiet replace ya1_productos=ya1_productos+ 650000 if d08t==13 & rest==1
			quiet replace ya1_productos=ya1_productos+ 750000 if d08t==14 & rest==1
			quiet replace ya1_productos=ya1_productos+ 850000 if d08t==15 & rest==1
			quiet replace ya1_productos=ya1_productos+ 950000 if d08t==16 & rest==1
			quiet replace ya1_productos=ya1_productos+ 1250000 if d08t==17 & rest==1
			quiet replace ya1_productos=ya1_productos+ 2250000 if d08t==18 & rest==1
			quiet summ d08m if (d08m>3000000 & d08m!=88 & d08m!=99) , de 
			quiet replace ya1_productos =ya1_productos + max(3000000,r(p50)) if d08t==19 & rest==1
			replace ya1_productos=. if rest==1 & (d08t==88 | d08t==99)
			drop rest

			
	// II.2. Labor income - secondary occupation
			* Labor income - secondary occupation - wage (monthly for wage earners according to d10)
			quiet gen ya2_sueldo=0 if ind_c2010==1  
			quiet replace ya2_sueldo= ya2_sueldo + d10m if (d10m!=. & d10m!=88 & d10m!=99)
			quiet summ d10m if (d10m<=5000 & d10m!=88 & d10m!=99) , de 
			qui gen rest = 1 if (d10m==. | d10m==88 | d10m==99)
			quiet replace ya2_sueldo =ya2_sueldo + r(p50) if d10t==1 & rest==1
			quiet replace ya2_sueldo =ya2_sueldo + 7500 if d10t==2 & rest==1
			quiet replace ya2_sueldo =ya2_sueldo + 12500 if d10t==3 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo + 22500 if d10t==4 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo + 40000 if d10t==5 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo + 75000 if d10t==6 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo + 125000 if d10t==7 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo + 175000 if d10t==8 & rest==1
			quiet replace ya2_sueldo= ya2_sueldo + 250000 if d10t==9 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo + 350000 if d10t==10 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo+ 450000 if d10t==11 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo+ 550000 if d10t==12 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo+ 650000 if d10t==13 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo+ 750000 if d10t==14 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo+ 850000 if d10t==15 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo+ 950000 if d10t==16 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo+ 1250000 if d10t==17 & rest==1
			quiet replace ya2_sueldo=ya2_sueldo+ 2250000 if d10t==18 & rest==1
			quiet summ d10m if (d10m>3000000 & d10m!=88 & d10m!=99) , de 
			replace ya2_sueldo =ya2_sueldo + max(3000000,r(p50)) if d10t==19 & rest==1 /*imputation of median or CLP 3000000*/
			replace ya2_sueldo=. if rest==1 & (d10t==88 | d10t==99)
			drop rest
			replace ya2_sueldo=. if (d09==1 | d09==2 | d09==3) & d10m==. & d10t==.
			
			
		** Imputation of missing values for observations with no information on work variables (d01, d02, d03)
		foreach x in ya1_sueldo ya1_negocio ya1_productos ya2_sueldo {
			replace `x' = . if ((d01!=1 & d01!=2) & (d02!=1 & d02!=2) & (d03!=1 & d03!=2)) ///
								| ((d01==2) & (d02!=1 & d02!=2) & (d03!=1 & d03!=2)) ///
								| ((d01==2) & (d02==2) & (d03!=1 & d03!=2)) 
		}					
			

		** Grouping of income coming from primary and secondary occupation
		egen ya1=  rsum(ya1_sueldo ya1_negocio ya1_productos) if ya1_sueldo!=. | ya1_negocio!=. | ya1_productos!=.
		gen ya2 = ya2_sueldo
		egen ylaborales = rsum(ya1 ya2) if ya1!=. | ya2!=.
		
		
	// II.3. Total Labor Income
	
		replace ylaborales=. if ya1_negocio==. &  ya1_productos==. & (d09==4 | d09==88 | d09==88)
		replace ylaborales=. if ya1_negocio==. &  ya1_productos==. & ya2_sueldo==.
		replace ylaborales=. if ya1_sueldo==. & (d09==4 | d09==88 | d09==88)
		replace ylaborales=. if ya1_sueldo==. & ya2_sueldo==.

		
	// II.4. Income coming from dependent and independent activities	
		
		gen ydeptes= ya1_sueldo
		replace ydeptes = ydeptes + ya2_sueldo if d09==3 & ya2_sueldo!=. 
		egen yindeptes= rsum(ya1_negocio ya1_productos) if ya1_negocio!=. | ya1_productos!=.
		replace yindeptes = yindeptes + ya2_sueldo if (d09==1 | d09==2) & ya2_sueldo!=.  
		
		
	** Conversion of monetary variables to November 2009 chilean pesos
	foreach y in ya1_sueldo ya1_negocio ya1_productos ya2_sueldo ya1 ya2 ylaborales ydeptes yindeptes  {
		replace `y'=`y'*(ipc_nov2009/ipc)
	}

	
// ////////////////////////////////////////////////////////////////////////		
// III. Wage employment, Self-employment and Employment (defined by income of those activities)
// ////////////////////////////////////////////////////////////////////////	

	
		gen dependiente = (ydeptes>0) if ind_c2010==1 & ydeptes!=. & yindeptes!=.
		gen independiente = (yindeptes>0) if ind_c2010==1 & ydeptes!=. & yindeptes!=.
		gen trabaja=(ylaborales>0) if ind_c2010==1 & ylaborales!=.
	
		

// ////////////////////////////////////////////////////////////////////////	
// IV. Demographics
// ////////////////////////////////////////////////////////////////////////	
	
	** Household size
	quiet egen numper= count(orden) if ind_c2010==1, by(id)
	
	
	** Number of family cores in household
	egen nucleos= max(a4) if ind_c2010==1, by(id)
	
	** Years of education
	quiet gen esc= 0 if (b05t<=2 | b05t==7)
	quiet replace esc=b05c if (b05t==3 | b05t==5) & (b05c!=88 & b05c!=99 & b05c!=.)
	quiet replace esc=6 if b05t==4
	quiet replace esc=8 if b05t==6
	quiet replace esc=6 + b05c if (b05t==8 | b05t==12) & (b05c!=88 & b05c!=99 & b05c!=.)
	quiet replace esc=12 if b05t==9 | b05t==11 | b05t==13 | b05t==15
	quiet replace esc=8 + b05c if (b05t==10 | b05t==14) & (b05c!=88 & b05c!=99 & b05c!=.)
	quiet replace esc=12 + b05c if (b05t==16 | b05t==17 | b05t==18) & (b05c!=88 & b05c!=99 & b05c!=.)
	quiet replace esc=17 + b05c if b05t==19 & (b05c!=88 & b05c!=99 & b05c!=.)
	
	
	** Educational level
	gen basica= (b05t<=7) if (b05t!=88 & b05t!=99 & b05t!=.)
	gen media_inc= (b05t==8 | b05t==10 | b05t==12 | b05t==14) if (b05t!=88 & b05t!=99 & b05t!=.)
	gen media= (b05t==9 | b05t==11 | b05t==13 | b05t==15) if (b05t!=88 & b05t!=99 & b05t!=.)
	gen superior= (b05t>=16 & b05t<=19) if (b05t!=88 & b05t!=99 & b05t!=.)
		tab media
		tab sup

	
	** Age
	gen edad =  a3 
	
		
	** Gender (1 male, 0 female)
	generate sexo= a2
	recode sexo (2 = 0)
	label define sexolbl 1 "Male" 0 "Female"
	label values sexo sexolbl
	label var sexo "Dummy Gender (1=Male)"
	
		
** Information only for beneficiaries

	 keep if orden==1	

	 
// ////////////////////////////////////////////////////////////////////////	
// V. Labeling
// ////////////////////////////////////////////////////////////////////////	

label var dependiente "Wage employment"
label var independiente "Self-employment"
label var trabaja "Total employment"
label var ylaborales "Total labor income"
label var ydeptes "Wage employment income"
label var yindeptes "Self employment income"
label var basica "Primary education"
label var media_inc "Secondary education incomplete"
label var media "Secondary educaction complete"
label var superior "Tertiary education"
label var esc "Years of education"
label var numper "Household size"
label var nucleos "Number of family cores in household"


***********************************************************************************************************	
*** Saving database

	keep id orden ind_c2010 mesp mesp_p C anymesp TT numper nucleos sexo edad esc basica media_inc media superior ydeptes yindeptes ylaborales independiente dependiente trabaja gradrate qualityindex fichaps
	
	sort id orden
	bysort id : keep if _n== 1
	
	drop orden

	sort id
			
	save "${path_data}/variables2010", replace
	
		
log close
	
	
	

	
